<template>
	<view class="container">
		<view class="edit" v-if="edit">
			<view class="uni-form-item uni-column">
				<view class="title">姓名</view>
				<input class="uni-input" v-model="address.name" placeholder="请输入姓名" />
			</view>
			<view class="uni-form-item uni-column">
				<view class="title">手机号</view>
				<input class="uni-input" v-model="address.mobile" placeholder="请输入手机号" />
			</view>
			<view class="uni-form-item uni-column">
				<view class="title">地址</view>
				<input class="uni-input" v-model="address.address" @focus="showMulLinkageThreePicker" placeholder="请选择地址"/>
			</view>
			<mpvue-city-picker :themeColor="themeColor" ref="mpvueCityPicker" :pickerValueDefault="address.cityPickerValueDefault"
			@onConfirm="onConfirm"></mpvue-city-picker>
			<view class="uni-form-item uni-column">
				<view class="title">详细地址</view>
				<input class="uni-input" v-model="address.detail" placeholder="请输入详细地址" />
			</view>
			<button class="feedback-submit" @tap="save">保存</button>
		</view>
		<view v-else>
			<view class="address-list">
				<view class="a-address" v-for="(item,index) in adds" :key="index">
					<view class="left-text" :class="item.is_default? 'active':''" @tap="selectTap(item.id)">
						<view class="name-tel">
							{{item.name}} {{item.mobile}}
						</view>
						<view class="address-box">
							{{item.address}}
						</view>
					</view>
					<view class="right-edit" @tap="editAddess(item)"></view>
				</view>
			</view>
			<view class="bottom-box">
				<!-- <view class="add-btn" @tap="addAddess">新增收货地址</view> -->
				<button class="feedback-submit" @tap="addAddess">新增收货地址</button>
			</view>
		</view>
		

	</view>
</template>

<script>
	import mpvuePicker from '../../components/mpvue-picker/mpvuePicker.vue';
	import mpvueCityPicker from '../../components/mpvue-citypicker/mpvueCityPicker.vue'
	import cityData from '../../common/city.data.js';
	import {
		mapState, 
		mapGetters,
		mapMutations,
		mapActions
	} from 'vuex'
	export default {
		components: {
			mpvuePicker,
			mpvueCityPicker
		},
		data() {
			return {
				address: {
					consignee: '',
					name: '',
					mobile: '',
					address: '',
					detail: '',
					region: '',
					cityPickerValueDefault: [0, 0, 1],
				},
				themeColor: '#FF80AB',
				edit: false,
				
			}
		},
		computed: {
			...mapGetters(["adds"])
		},
		onLoad() {
			this.getAdd();
		},
		methods: {
			...mapActions(['setDefault','addAdd','updateAdd','getAdd']),
			onConfirm(e) {
				// this.pickerText = JSON.stringify(e)
				console.log(JSON.stringify(e))
				this.address.address = e.label;
				this.address.region = e.cityCode;
			},
			showMulLinkageThreePicker() {
				this.$refs.mpvueCityPicker.show()
			},
			selectTap(id) {
				console.log("tap item id:" + JSON.stringify(id));
				this.setDefault({consignee:id}).then(()=>{
					this.getAdd();
					uni.showToast({title: '设置成功'});
				})
			},
			editAddess(item) {
				let arr = item.address.split(" ");
				this.address.consignee = item.id;
				this.address.name = item.name;
				this.address.mobile = item.mobile;
				this.address.address = arr.length>1?arr[0]:"";
				this.address.detail = arr.length>1?arr[1]:"";
				
				this.edit = !this.edit;
			},
			addAddess() {
				this.address.consignee = '';
				this.address.name = '';
				this.address.mobile = '';
				this.address.address = '';
				this.address.detail = '';
				this.edit = !this.edit;
				console.log("tap add new Address");
			},
			save() {
				let param = {
					name:this.address.name,
					tel: this.address.mobile,
					mobile: this.address.mobile,
					address: this.address.address + " " + this.address.detail,
					region: "405"
				}
				this.edit = !this.edit;
				if(this.address.consignee){
					param.consignee = this.address.consignee;
					this.updateAdd(param).then(()=>{
						this.getAdd();
					})
					return;
				}
				this.addAdd(param).then(()=>{
					this.getAdd();
				})
				
			}
		}
	}
</script>

<style lang="scss">
	.container {
		padding: 10upx;
		background-color: #F2f2f2;
	}

	.address-list {
		width: 100%;
		// background-color: #fff;
		margin-top: 20upx;
		padding-bottom: 100upx;
	}

	.address-list .a-address {
		width: 720upx;
		margin-left: 30upx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30upx 0;
		border-bottom: 1upx solid #eee;
	}

	.a-address .left-text {
		width: 610upx;
		box-sizing: border-box;
		padding-left: 58upx;
	}

	.a-address .left-text.active {
		background: url() no-repeat left center;
		background-size: 28upx auto;
	}

	.a-address .left-text .name-tel {
		margin-bottom: 20upx;
	}

	.a-address .left-text .address-box {
		font-size: 24upx;
		color: #888888;
		line-height: 36upx;
	}

	.a-address .right-edit {
		width: 109upx;
		height: 100%;
		padding: 50upx 0 50upx 58upx;
		box-sizing: border-box;
		background: url() no-repeat 43upx center;
		background-size: 35upx auto;
	}
	
	.bottom-box {
		position: fixed;
		width: 100%;
		bottom: 0;
		font-size: 28upx;
		color: #000000;
		// border-top: 1upx solid #eee;
		// height: 100upx;
		line-height: 100upx;
		// background: #fff url() no-repeat 705upx center;
		background-size: 15upx auto;
	}

	.bottom-box .add-btn {
		// margin-left: 30upx;
		padding-left: 70upx;
		// background: url() no-repeat left center;
		background-size: 40upx auto;
	}
	
	.feedback-submit {
		background-color: $uni-theme;
		color: #fff;
		margin: 20upx;
	}
</style>
